package java_generics.generic_methods;

public class NewInstanceTest {

	public static void main(String[] args) {
		try {
			A a = getInstance(A.class); // OK
			System.out.println(a.toString());

			B b = getInstance(B.class); // Exception
			System.out.println(b.toString());
		} catch (InstanceException ex) {
			System.out.println(ex.getMessage());
		}
	}

	static <T> T getInstance(Class<T> c) {
		try {
			T instance = c.newInstance();
			return instance;
		} catch (IllegalAccessException ex) {
			throw new InstanceException();
		} catch (InstantiationException ex) {
			throw new InstanceException();
		}
	}
}
